//
//  XZResManagementVC.m
//  Teecloud
//
//  Created by PC on 2018/10/22.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZResManagementVC.h"
#import "XZResManagementCell.h"
#import "XZResManagemen.h"
#import "XZModifyPhoneVC.h"
@interface XZResManagementVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end
#define RESMANAGER  @"XZResManagementCell"
@implementation XZResManagementVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"付费资源管理";
    self.view.backgroundColor = kWhiteColor;
    [self pullNewsData];
}
//下拉刷新
- (void)pullNewsData{
    self.pageIndex = 0;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:@(self.pageIndex) forKey:@"pageIndex"];
    
    __weak typeof(self)weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetMyFeeResList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [weakSelf.tabView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                if (dataInfo.count > 0) {
                    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
                    self.navigationItem.rightBarButtonItem = barItem;
                }
                NSArray *newsArray = [XZResManagemen mj_objectArrayWithKeyValuesArray:dataInfo];
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:newsArray];
                [weakSelf.tabView.mj_footer endRefreshing];
                [weakSelf.tabView.mj_header endRefreshing];
                if (weakSelf.dataSource.count == 0) {
                    [weakSelf.tabView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
                        
                    } butClick:^{
                        
                    }];
                }
                [weakSelf.tabView reloadData];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
            
        }else{
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakSelf.view];
        }
    }];
}
- (void)addMoreNewsData{
    self.pageIndex ++;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:@(self.pageIndex) forKey:@"pageIndex"];
    
    __weak typeof(self)weakSelf = self;
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetMyFeeResList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                NSArray *newsArray = [XZResManagemen mj_objectArrayWithKeyValuesArray:dataInfo];
                [weakSelf.dataSource addObjectsFromArray:newsArray];
                if (dataInfo.count>0) {
                    [weakSelf.tabView.mj_footer endRefreshing];
                    [weakSelf.tabView.mj_header endRefreshing];
                    [weakSelf.tabView reloadData];
                }else{//没有数据的时候就显示暂无数据
                    weakSelf.tabView.mj_footer.state = MJRefreshStateNoMoreData;
                }
                
            }else{
                [weakSelf.tabView.mj_footer endRefreshing];
                [weakSelf.tabView.mj_header endRefreshing];
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
        }else{
            [weakSelf.tabView.mj_footer endRefreshing];
            [weakSelf.tabView.mj_header endRefreshing];
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakSelf.view];
        }
    }];
}
- (void)modifyPhone{
    XZModifyPhoneVC *modifyPhoneVC = [[XZModifyPhoneVC alloc]init];
    [self.navigationController pushViewController:modifyPhoneVC animated:true];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tabView.mj_footer.hidden = self.dataSource.count > 0? NO:YES;
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XZResManagementCell *cell = [tableView dequeueReusableCellWithIdentifier:RESMANAGER];
    XZResManagemen *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.f;
}

#pragma mark - setter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if (!_tabView) {
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-kNavBarAndStatusBarHeight-kBottomSafeHeight) style:UITableViewStylePlain];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.showsVerticalScrollIndicator =false;
        _tabView.separatorStyle =UITableViewCellSeparatorStyleNone;
        [_tabView registerNib:[UINib nibWithNibName:@"XZResManagementCell" bundle:nil] forCellReuseIdentifier:RESMANAGER];
        //上下拉刷新
        _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullNewsData)];
        _tabView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addMoreNewsData)];
        _tabView.mj_footer.hidden =YES;
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
//申请变更手机号型号Btn
- (UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(0, 0, 100, 44);
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:10.f];
        [_rightBtn setTitle:@"申请变更绑定手机型号" forState:0];
        _rightBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [_rightBtn setTitleColor:kWhiteColor forState:0];
        [_rightBtn addTarget:self action:@selector(modifyPhone) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}
@end
